<?xml version="1.0"?>
<!DOCTYPE xsl:stylesheet>

<!--
 2007-12-25
 Copyright (c) Art. Lebedev | http://www.artlebedev.ru/
 Author - Vladimir Tokmakov
-->

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">


	<xsl:variable name="f_fieldset_id_prefix"></xsl:variable>
	<xsl:variable name="f_legend_prefix">l_</xsl:variable>

	<xsl:variable name="f_row_id_prefix">r_</xsl:variable>
	<xsl:variable name="f_field_id_prefix"></xsl:variable>
	<xsl:variable name="f_option_suffix">_i</xsl:variable>

	<xsl:variable name="f_repeat_append_prefix">ra_</xsl:variable>
	<xsl:variable name="f_repeat_remove_prefix">rr_</xsl:variable>
	<xsl:variable name="f_repeat_suffix">_</xsl:variable>

	<xsl:variable name="f_ghost_id_prefix">s_</xsl:variable>
	<xsl:variable name="f_sheet_previous_prefix">sp_</xsl:variable>
	<xsl:variable name="f_sheet_next_prefix">sn_</xsl:variable>

	<xsl:variable name="f_apostrophe_symbol">'</xsl:variable>


	<xsl:template match="form" mode="f_make_form"><!-- Will be removed -->
		<xsl:param name="data" select="." />
		<xsl:apply-templates select="." mode="f_form">
			<xsl:with-param name="data" select="$data" />
		</xsl:apply-templates>
	</xsl:template>


	<xsl:template match="form" mode="f_form">
		<xsl:param name="data" select="." />
		<xsl:variable name="id">
			<xsl:apply-templates select="." mode="f_get_ID" />
		</xsl:variable>
		<xsl:call-template name="f_form_begin">
			<xsl:with-param name="id" select="$id" />
		</xsl:call-template>
		<xsl:apply-templates select="." mode="f_script">
			<xsl:with-param name="id" select="$id" />
		</xsl:apply-templates>
		<form>
			<xsl:call-template name="f_form_attributes">
				<xsl:with-param name="form" select="." />
				<xsl:with-param name="id" select="$id" />
				<xsl:with-param name="data" select="$data" />
			</xsl:call-template>
			<input type="hidden" name="submitted" value="true" />
			<xsl:call-template name="f_data">
				<xsl:with-param name="data" select="$data" />
				<xsl:with-param name="parent_id" select="$id" />
			</xsl:call-template>			
		</form>
		<xsl:call-template name="f_form_end">
			<xsl:with-param name="id" select="$id" />
			<xsl:with-param name="postfix">
				<xsl:value-of select="count(preceding::form) + 1" />
			</xsl:with-param>
		</xsl:call-template>
	</xsl:template>


	<xsl:template name="f_form_begin">
		<xsl:param name="id" />
		<style type="text/css">
#<xsl:value-of select="$id" />{visibility:hidden;}
#<xsl:value-of select="$id" />.widget-inited{visibility:visible;}
</style>
		<noscript>
			<style type="text/css">
body #<xsl:value-of select="$id" />{visibility:visible;}
</style>
		</noscript>
		<script type="text/javascript">
var aForm = new Array();
</script>
	</xsl:template>


	<xsl:template name="f_form_end">
		<xsl:param name="id" />
		<xsl:param name="postfix" />		
		<script type="text/javascript">
try{
	var xForm<xsl:value-of select="$postfix" /> = new FormBuilder(aForm).getForm();
	xForm<xsl:value-of select="$postfix" />.init();
}catch(e){
	document.getElementById('<xsl:value-of select="$id" />').className+=' widget-inited';
	throw(e);
}
</script>
	</xsl:template>


	<xsl:template name="f_form_attributes">
		<xsl:param name="form" />
		<xsl:param name="id" />
		<xsl:param name="data" />
		<xsl:attribute name="id"><xsl:value-of select="$id" /></xsl:attribute>
		<xsl:attribute name="name"><xsl:apply-templates select="$form" mode="f_get_name" /></xsl:attribute>
		<xsl:copy-of select="$form/@action | $form/@method | $form/@enctype | $form/@accept-charset | $form/@accept | $form/@onsubmit | $form/@onreset | $form/@class | $form/@autocomplete" />
		<xsl:if test="not( string( $form/@method ) )">
			<xsl:attribute name="method">post</xsl:attribute>
		</xsl:if>
		<xsl:if test="$data/descendant::input[@type = 'file']">
			<xsl:attribute name="method">post</xsl:attribute>
			<xsl:attribute name="enctype">multipart/form-data</xsl:attribute>
		</xsl:if>
	</xsl:template>


	<!-- javascript -->
	<xsl:template match="*" mode="f_script">
		<xsl:param name="id" select="''" />
		<xsl:param name="parent_id" select="''" />
		<xsl:param name="row_id" select="''" />
		<xsl:param name="repeat_template" />
		<xsl:param name="reset_value" />
		<xsl:param name="position" select="0" />
		<xsl:param name="combobox" />
		<xsl:param name="slider" />
		<xsl:variable name="current" select="." />
		<xsl:text><![CDATA[
]]></xsl:text>
		<script>
			<xsl:text>aForm[aForm.length]={sID:'</xsl:text>
				<xsl:choose>
					<xsl:when test="@type = 'checkbox' or @type = 'radio'">
						<xsl:value-of select="$row_id" />
					</xsl:when>
					<xsl:otherwise>
						<xsl:value-of select="$id" />
					</xsl:otherwise>
				</xsl:choose>
				<xsl:text>',</xsl:text>
				<xsl:if test="string( $parent_id )">sParent_ID:'<xsl:value-of select="$parent_id" />',</xsl:if>
				<xsl:if test="string( $row_id ) and not( @type = 'checkbox' or @type = 'radio' )">sRow_ID:'<xsl:value-of select="$row_id" />',</xsl:if>
				<xsl:variable name="params" select="following-sibling::*[1][name() = 'dl'] | *[1][name() = 'dl']" />
				<xsl:variable name="data" select="$params/dt" />
				<xsl:if test="$combobox">
					<xsl:text>sFrom_ID:'</xsl:text>
						<xsl:apply-templates select="$combobox" mode="f_get_ID">
							<xsl:with-param name="position" select="$position" />
						</xsl:apply-templates>
					<xsl:text>',</xsl:text>
				</xsl:if>
				<xsl:if test="name() = 'fieldset' and ( contains( @class, 'sheet' ) and not( contains( @class, 'tabs_sheet' ) ) )">
					<xsl:text>oSheet:{bSelected:</xsl:text>
					<xsl:choose>
						<xsl:when test="contains( @class, 'selected' ) or ( not( preceding-sibling::fieldset[( contains( @class, 'sheet' ) and not( contains( @class, 'tabs_sheet' ) ) )] | ../fieldset[contains( @class, 'selected' )] ) )">
							<xsl:text>true</xsl:text>
						</xsl:when>
						<xsl:otherwise>
							<xsl:text>false</xsl:text>
						</xsl:otherwise>
					</xsl:choose>
					<xsl:if test="string( legend )">
						<xsl:text>,sLegend_ID:'</xsl:text>
						<xsl:value-of select="concat( $f_legend_prefix, $id )" />
						<xsl:text>'</xsl:text>
					</xsl:if>
					<xsl:text>,sParent_ID:'</xsl:text>
					<xsl:choose>
						<xsl:when test="preceding-sibling::fieldset[( contains( @class, 'sheet' ) and not( contains( @class, 'tabs_sheet' ) ) )]">
							<xsl:value-of select="concat( $f_ghost_id_prefix, generate-id( preceding-sibling::fieldset[( contains( @class, 'sheet' ) and not( contains( @class, 'tabs_sheet' ) ) )][last()] ) )" />
						</xsl:when>
						<xsl:otherwise>
							<xsl:value-of select="concat( $f_ghost_id_prefix, generate-id( . ) )" />
						</xsl:otherwise>
					</xsl:choose>
					<xsl:text>'</xsl:text>
					<xsl:if test="preceding-sibling::*[name() = name( current() ) and ( contains( @class, 'sheet' ) and not( contains( @class, 'tabs_sheet' ) ) )]">
						<xsl:text>,sPrev_ID:'</xsl:text>
						<xsl:value-of select="concat( $f_sheet_previous_prefix, $id )" />
						<xsl:text>'</xsl:text>
					</xsl:if>
					<xsl:if test="following-sibling::*[name() = name( current() ) and ( contains( @class, 'sheet' ) and not( contains( @class, 'tabs_sheet' ) ) )]">
						<xsl:text>,sNext_ID:'</xsl:text>
						<xsl:value-of select="concat( $f_sheet_next_prefix, $id )" />
						<xsl:text>'</xsl:text>
					</xsl:if>
					<xsl:text>}, </xsl:text>
				</xsl:if>
				<xsl:if test="@type = 'radio' or @type = 'checkbox'">
					<xsl:text>asOption_ID:[</xsl:text>
						<xsl:for-each select="descendant::option">
							<xsl:variable name="option_id">
								<xsl:apply-templates select="." mode="f_get_ID">
									<xsl:with-param name="position" select="$position" />
								</xsl:apply-templates>
							</xsl:variable>
							<xsl:if test="position() > 1">,</xsl:if>
							<xsl:text>['</xsl:text>
								<xsl:value-of select="$option_id" />
							<xsl:text>','</xsl:text>
								<xsl:value-of select="concat( $f_row_id_prefix, $option_id )" />
							<xsl:text>']</xsl:text>
						</xsl:for-each>
					<xsl:text>],</xsl:text>
				</xsl:if>
				<xsl:if test="$data[contains( text(), 'submit' )]">
					<xsl:text>oSubmit:{bValid:</xsl:text>
					<xsl:choose>
						<xsl:when test="$data[text() = 'submit']/following-sibling::*[1][name() = 'dd']/text() = 'valid'">
							<xsl:text>true</xsl:text>
						</xsl:when>
						<xsl:otherwise>false</xsl:otherwise>
					</xsl:choose>
					<xsl:text>,bDisabled_button:</xsl:text>
					<xsl:choose>
						<xsl:when test="$data[text() = 'submit_button']/following-sibling::*[1][name() = 'dd']/text() = 'enabled'">
							<xsl:text>false</xsl:text>
						</xsl:when>
						<xsl:otherwise>true</xsl:otherwise>
					</xsl:choose>
					<xsl:text>,bChanged:</xsl:text>
					<xsl:choose>
						<xsl:when test="$data[text() = 'submit']/following-sibling::dd/text() = 'changed'">
							<xsl:text>true</xsl:text>
						</xsl:when>
						<xsl:otherwise>false</xsl:otherwise>
					</xsl:choose>
					<xsl:text>}, </xsl:text>
				</xsl:if>
				<xsl:variable name="options_depended" select="$data[contains( text(), 'options' )]" />
				<xsl:if test="$data[contains( text(), 'depended' ) and not( contains( text(), 'options' ) )]">
					<xsl:text>oDepended:{sLogic:'</xsl:text>
						<xsl:choose>
							<xsl:when test="$data[contains( text(), 'depended' ) and contains( text(), 'and' ) and not( contains( text(), 'options' ) )]">and</xsl:when>
							<xsl:otherwise>or</xsl:otherwise>
						</xsl:choose>
					<xsl:text>'</xsl:text>
					<xsl:if test="contains( text(), 'select' )">,bSelect:true</xsl:if>
					<xsl:if test="contains( text(), 'value' )">,bValue:true</xsl:if>
					<xsl:text>,aFrom:[</xsl:text>
					<xsl:for-each select="$data[contains( text(), 'depended_from' ) and not( contains( text(), 'options' ) )]">
						<xsl:variable name="dt_id" select="generate-id( . )" />
						<xsl:variable name="dd" select="following-sibling::dd[generate-id( preceding-sibling::dt[1] ) = $dt_id]" />
						<xsl:if test="position() > 1">,</xsl:if>
						<xsl:text>{sName:'</xsl:text>
						<xsl:variable name="repeated"
							select="$current/ancestor::*[following-sibling::*[1][name() = 'dl'][dt = 'repeat']]/descendant::*[@name = $dd[1]]" />
						<xsl:choose>
							<xsl:when test="$repeated">
								<xsl:apply-templates select="$repeated" mode="f_get_name">
									<xsl:with-param name="position" select="$position" />
								</xsl:apply-templates>
							</xsl:when>
							<xsl:otherwise>
								<xsl:value-of select="$dd[1]" />
							</xsl:otherwise>
						</xsl:choose>
						<xsl:text>'</xsl:text>
						<xsl:if test="contains( text(), 'inverse' )">,bInverse:true</xsl:if>
						<xsl:if test="$dd[2]">
							<xsl:text>,mData:</xsl:text>
							<xsl:apply-templates select="$dd[2]" mode="f_script" />
						</xsl:if>
						<xsl:text>}</xsl:text>
					</xsl:for-each>
					<xsl:text>]}, </xsl:text>
				</xsl:if>
				<xsl:if test="$data[contains( text(), 'options_depended' )]">
					<xsl:text>oOptions_depended:{sLogic:'</xsl:text>
						<xsl:choose>
							<xsl:when test="$data[contains( text(), 'options_depended' ) and contains( text(), 'and' )]">and</xsl:when>
							<xsl:otherwise>or</xsl:otherwise>
						</xsl:choose>
					<xsl:text>'</xsl:text>
					<xsl:if test="contains( text(), 'select' )">,bSelect:true</xsl:if>
					<xsl:if test="contains( text(), 'value' )">,bValue:true</xsl:if>
					<xsl:text>,aFrom:[</xsl:text>
					<xsl:for-each select="$data[contains( text(), 'options_depended_from' )]">
						<xsl:variable name="dt_id" select="generate-id( . )" />
						<xsl:variable name="dd" select="following-sibling::dd[generate-id( preceding-sibling::dt[1] ) = $dt_id]" />
						<xsl:if test="position() > 1">, </xsl:if>
						<xsl:text>{sName:'</xsl:text>
						<xsl:variable name="repeated"
							select="$current/ancestor::*[following-sibling::*[1][name() = 'dl'][dt = 'repeat']]/descendant::*[@name = $dd[1]]" />
						<xsl:choose>
							<xsl:when test="$repeated">
								<xsl:apply-templates select="$repeated" mode="f_get_name">
									<xsl:with-param name="position" select="$position" />
								</xsl:apply-templates>
							</xsl:when>
							<xsl:otherwise>
								<xsl:value-of select="$dd[1]" />
							</xsl:otherwise>
						</xsl:choose>
						<xsl:text>'</xsl:text>
						<xsl:if test="contains( text(), 'inverse' )">,bInverse:true</xsl:if>
						<xsl:if test="$dd[2]">
							<xsl:text>,mData:</xsl:text>
								<xsl:text>[</xsl:text>
								<xsl:for-each select="$dd[position() > 1]">
									<xsl:if test="position() mod 2 = 1">
										<xsl:if test="position() > 1">,</xsl:if>
										<xsl:text>[</xsl:text>
										<xsl:apply-templates select="." mode="f_script" />
										<xsl:text>,</xsl:text>
										<xsl:apply-templates select="following-sibling::*[1][name() = 'dd']" mode="f_script" />
										<xsl:text>]</xsl:text>
									</xsl:if>
								</xsl:for-each>
								<xsl:text>]</xsl:text>
						</xsl:if>
						<xsl:text>}</xsl:text>
					</xsl:for-each>
					<xsl:text>]}, </xsl:text>
				</xsl:if>
				<xsl:variable name="class_from" select="$data[contains( text(), 'class' )]" />
				<xsl:if test="$class_from">
					<xsl:text>oClass:{aFrom:[</xsl:text>
					<xsl:for-each select="$data[contains( text(), 'class_from' )]">
						<xsl:variable name="dt_id" select="generate-id( . )" />
						<xsl:variable name="dd" select="following-sibling::dd[generate-id( preceding-sibling::dt[1] ) = $dt_id]" />
						<xsl:if test="position() > 1">,</xsl:if>
						<xsl:text>{sName:'</xsl:text>
						<xsl:variable name="repeated"
							select="$current/ancestor::*[following-sibling::*[1][name() = 'dl'][dt = 'repeat']]/descendant::*[@name = $dd[1]]" />
						<xsl:choose>
							<xsl:when test="$repeated">
								<xsl:apply-templates select="$repeated" mode="f_get_name">
									<xsl:with-param name="position" select="$position" />
								</xsl:apply-templates>
							</xsl:when>
							<xsl:otherwise>
								<xsl:value-of select="$dd[1]" />
							</xsl:otherwise>
						</xsl:choose>
						<xsl:text>'</xsl:text>
						<xsl:if test="contains( text(), 'inverse' )">,bInverse:true</xsl:if>
						<xsl:if test="$dd[2]">
							<xsl:text>,mData:</xsl:text>
							<xsl:choose>
								<xsl:when test="$class_from">
									<xsl:text>[</xsl:text>
									<xsl:for-each select="$dd[position() > 1]">
										<xsl:if test="position() mod 2 = 1">
											<xsl:if test="position() > 1">,</xsl:if>
											<xsl:text>[</xsl:text>
											<xsl:apply-templates select="." mode="f_script" />
											<xsl:text>,</xsl:text>
											<xsl:apply-templates select="following-sibling::*[1][name() = 'dd']" mode="f_script" />
											<xsl:text>]</xsl:text>
										</xsl:if>
									</xsl:for-each>
									<xsl:text>]</xsl:text>
								</xsl:when>
								<xsl:otherwise>
									<xsl:apply-templates select="$dd[2]" mode="f_script" />
								</xsl:otherwise>
							</xsl:choose>
						</xsl:if>
						<xsl:text>}</xsl:text>
					</xsl:for-each>
					<xsl:text>]}, </xsl:text>
				</xsl:if>
				<xsl:if test="$data[text() = 'valid']">
					<xsl:text>oValid:{</xsl:text>
						<xsl:text>sLogic:'</xsl:text>
							<xsl:choose>
								<xsl:when test="$data[contains( text(), 'valid' ) and contains( text(), 'and' )]">and</xsl:when>
								<xsl:otherwise>or</xsl:otherwise>
							</xsl:choose>
						<xsl:text>'</xsl:text>
						<xsl:text>,aFrom:[</xsl:text>
						<xsl:for-each select="$data[contains( text(), 'valid' )]">
							<xsl:variable name="dt_id" select="generate-id( . )" />
							<xsl:variable name="dd" select="following-sibling::dd[generate-id( preceding-sibling::dt[1] ) = $dt_id]" />
							<xsl:if test="position() > 1">,</xsl:if>
							<xsl:text>{</xsl:text>
							<xsl:if test="contains( text(), 'valid_from' )">
								<xsl:text>sName:'</xsl:text>
								<xsl:variable name="repeated"
									select="$current/ancestor::*[following-sibling::*[1][name() = 'dl'][dt = 'repeat']]/descendant::*[@name = $dd[1]]" />
								<xsl:choose>
									<xsl:when test="$repeated">
										<xsl:apply-templates select="$repeated" mode="f_get_name">
											<xsl:with-param name="position" select="$position" />
										</xsl:apply-templates>
									</xsl:when>
									<xsl:otherwise>
										<xsl:value-of select="$dd[1]" />
									</xsl:otherwise>
								</xsl:choose>
								<xsl:text>',</xsl:text>
							</xsl:if>
							<xsl:if test="contains( text(), 'inverse' )">bInverse:true,</xsl:if>
							<xsl:choose>
								<xsl:when test="contains( text(), 'valid_from' ) and $dd[2]">
									<xsl:text>mData:</xsl:text>
									<xsl:apply-templates select="$dd[2]" mode="f_script" />
								</xsl:when>
								<xsl:when test="not( contains( text(), 'valid_from' ) ) and $dd[1]">
									<xsl:text>mData:</xsl:text>
									<xsl:apply-templates select="$dd[1]" mode="f_script" />
								</xsl:when>
							</xsl:choose>
							<xsl:text>}</xsl:text>
						</xsl:for-each>
					<xsl:text>]},</xsl:text>
				</xsl:if>
				<xsl:if test="$data[text() = 'move_before_id']">
					<xsl:text>oMove:{sBefore_ID:'</xsl:text>
					<xsl:value-of select="$f_row_id_prefix" />
					<xsl:variable name="dt_id" select="generate-id( $data[text() = 'move_before_id'] )" />
					<xsl:variable name="dd" select="$data[text() = 'move_before_id']/following-sibling::dd[generate-id( preceding-sibling::dt[1] ) = $dt_id]" />
					<xsl:variable name="repeated"
						select="ancestor::*[following-sibling::*[1][name() = 'dl'][dt = 'repeat']]/descendant::*[@id = $dd[1]]" />
					<xsl:choose>
						<xsl:when test="$repeated and number( $position )">
							<xsl:value-of select="concat( $dd[1], $f_repeat_suffix, $position )" />
						</xsl:when>
						<xsl:otherwise>
							<xsl:value-of select="$dd[1]" />
						</xsl:otherwise>
					</xsl:choose>
					<xsl:text>'}, </xsl:text>
				</xsl:if>
				<xsl:if test="$data[contains( text(), 'required' )]">
					<xsl:text>oRequired:{sLogic:'</xsl:text>
						<xsl:choose>
							<xsl:when test="$data[contains( text(), 'required' ) and contains( text(), 'and' )]">and</xsl:when>
							<xsl:otherwise>or</xsl:otherwise>
						</xsl:choose>
					<xsl:text>'</xsl:text>
					<xsl:if test="$data[contains( text(), 'required_from' )]">
						<xsl:text>,aFrom:[</xsl:text>
						<xsl:for-each select="$data[contains( text(), 'required_from' )]">
							<xsl:variable name="dt_id" select="generate-id( . )" />
							<xsl:variable name="dd" select="following-sibling::dd[generate-id( preceding-sibling::dt[1] ) = $dt_id]" />
							<xsl:if test="position() > 1">,</xsl:if>
							<xsl:text>{sName:'</xsl:text>
							<xsl:value-of select="$dd[1]" />
							<xsl:text>'</xsl:text>
							<xsl:if test="contains( text(), 'inverse' )">,bInverse:true</xsl:if>
							<xsl:if test="$dd[2]">
								<xsl:text>,mData:</xsl:text>
								<xsl:choose>
									<xsl:when test="$options_depended">
										<xsl:text>[</xsl:text>
										<xsl:for-each select="$dd[position() > 1]">
											<xsl:if test="position() mod 2 = 1">
												<xsl:if test="position() > 1">,</xsl:if>
												<xsl:text>[</xsl:text>
												<xsl:apply-templates select="." mode="f_script" />
												<xsl:text>,</xsl:text>
												<xsl:apply-templates select="following-sibling::*[1][name() = 'dd']" mode="f_script" />
												<xsl:text>]</xsl:text>
											</xsl:if>
										</xsl:for-each>
										<xsl:text>]</xsl:text>
									</xsl:when>
									<xsl:otherwise>
										<xsl:apply-templates select="$dd[2]" mode="f_script" />
									</xsl:otherwise>
								</xsl:choose>
							</xsl:if>
							<xsl:text>}</xsl:text>
						</xsl:for-each>
						<xsl:text>]</xsl:text>
					</xsl:if>
					<xsl:for-each select="$data[( text() = 'required_min' or text() = 'required_max' ) and not( preceding-sibling::dt[text() = current()/text()] )]">
						<xsl:text>,</xsl:text>
						<xsl:variable name="text" select="substring-after( text(), 'required_' )" />
						<xsl:apply-templates select="." mode="f_script">
							<xsl:with-param name="name" select="concat( 'i', translate( $text, 'm', 'M' ) )" />
						</xsl:apply-templates>
					</xsl:for-each>
					<xsl:text>}, </xsl:text>
				</xsl:if>
				<xsl:if test="$data[contains( text(), 'repeat' )]">
					<xsl:text>oRepeat:{sID:'</xsl:text>
					<xsl:variable name="repeat_name" select="$data[text() = 'repeat' or text() = 'repeate_template']/following-sibling::*[1][name() = 'dd']" />
					<!--xsl:choose>
						<xsl:when test="$repeat_name">
							<xsl:value-of select="$repeat_name" />
						</xsl:when>
						<xsl:otherwise>
							<xsl:apply-templates select="." mode="f_get_name" />
						</xsl:otherwise>
					</xsl:choose-->
					<xsl:apply-templates select="." mode="f_get_ID">
						<!--xsl:with-param name="position" select="$position" /-->
					</xsl:apply-templates>
					<xsl:text>'</xsl:text>
					<xsl:for-each select="$data[( text() = 'repeat_min' or text() = 'repeat_max' ) and not( preceding-sibling::dt[text() = current()/text()] )]">
						<xsl:text>,</xsl:text>
						<xsl:apply-templates select="." mode="f_script">
							<xsl:with-param name="name" select="concat( 'i', translate( substring-after( text(), 'repeat_' ), 'm', 'M' ) )" />
						</xsl:apply-templates>
					</xsl:for-each>
					<xsl:text>,sAppend_ID:'</xsl:text>
					<xsl:value-of select="concat( $f_repeat_append_prefix, $id )" />
					<xsl:text>'</xsl:text>
					<xsl:text>,sRemove_ID:'</xsl:text>
					<xsl:value-of select="concat( $f_repeat_remove_prefix, $id )" />
					<xsl:text>'</xsl:text>
					<xsl:if test="$repeat_template">,bTemplate:true</xsl:if>
					<xsl:text>}, </xsl:text>
				</xsl:if>
				<xsl:if test="$slider and $data[starts-with( text(), 'slider' )]">
					<xsl:text>oSlider:{</xsl:text>
					<xsl:variable name="slider_type" select="$data[text() = 'slider']/following-sibling::*[position() = 1 and name() = 'dd']" />
					<xsl:if test="string( $slider_type )">
						<xsl:text>sType:'</xsl:text>
							<xsl:value-of select="$slider_type" />
						<xsl:text>',</xsl:text>
					</xsl:if>
					<xsl:text>aRules : [</xsl:text>
					<xsl:variable name="slider_min" select="$data[text() = 'min']/following-sibling::*[1][name() = 'dd']" />
					<xsl:variable name="slider_max" select="$data[text() = 'max']/following-sibling::*[1][name() = 'dd']" />
					<xsl:variable name="slider_step" select="$data[text() = 'step' and not( preceding-sibling::dt[text() = 'step'] )]/following-sibling::*[1][name() = 'dd']" />
					<xsl:choose>
						<xsl:when test="$data[text() = 'value']">
							<xsl:variable name="slider_marks_count" select="count( $data[text() = 'value'] ) - 1" />
							<xsl:for-each select="$data[text() = 'value']">
								<xsl:variable name="slider_position" select="position() - 1" />
								<xsl:variable name="slider_value" select="following-sibling::*[1][name() = 'dd']" />
								<xsl:text>{dValue:</xsl:text>
								<xsl:value-of select="$slider_value" />
								<xsl:text>,dPercent:</xsl:text>
								<xsl:variable name="slider_percent" select="$data[text() = 'percent' and count( preceding-sibling::dt[text() = 'percent'] ) = $slider_position]/following-sibling::*[1][name() = 'dd']" />
								<xsl:choose>
									<xsl:when test="position() = 1">0</xsl:when>
									<xsl:when test="position() = last()">100</xsl:when>
									<xsl:when test="$slider_percent">
										<xsl:value-of select="$slider_percent" />
									</xsl:when>
									<xsl:otherwise>
										<xsl:value-of select="floor( ( $slider_position ) * 100 div $slider_marks_count )" />
									</xsl:otherwise>
								</xsl:choose>
								<xsl:variable name="slider_step_local" select="$data[text() = 'step' and count( preceding-sibling::dt[text() = 'step'] ) = $slider_position]/following-sibling::*[1][name() = 'dd']" />
								<xsl:text>,dStep:</xsl:text>
								<xsl:choose>
									<xsl:when test="$slider_step_local">
										<xsl:value-of select="$slider_step_local" />
									</xsl:when>
									<xsl:when test="$slider_step">
										<xsl:value-of select="$slider_step" />
									</xsl:when>
									<xsl:when test="position() != last()">
										<xsl:value-of select="following-sibling::dt[text() = 'value'][1]/following-sibling::*[1][name() = 'dd'] - $slider_value" />
									</xsl:when>
									<xsl:otherwise>1</xsl:otherwise>
								</xsl:choose>
								<xsl:text>,sLabel:'</xsl:text>
								<xsl:choose>
									<xsl:when test="position() = 0 and $data[text() = 'min_label']">
										<xsl:value-of select="$data[text() = 'min_label']/following-sibling::*[1][name() = 'dd']" />
									</xsl:when>
									<xsl:when test="position() = last() and $data[text() = 'max_label']">
										<xsl:value-of select="$data[text() = 'max_label']/following-sibling::*[1][name() = 'dd']" />
									</xsl:when>
									<xsl:otherwise>
										<xsl:variable name="slider_label" select="$data[text() = 'label' and count( preceding-sibling::dt[text() = 'label'] ) = $slider_position]/following-sibling::*[1][name() = 'dd']" />
										<xsl:choose>
											<xsl:when test="$slider_label">
												<xsl:value-of select="$slider_label" />
											</xsl:when>
											<xsl:otherwise>
												<xsl:value-of select="$slider_value" />
											</xsl:otherwise>
										</xsl:choose>
									</xsl:otherwise>
								</xsl:choose>
								<xsl:text>'}</xsl:text>
								<xsl:if test="position() != last()">, </xsl:if>
							</xsl:for-each>
						</xsl:when>
						<xsl:when test="$slider_min and $slider_max and $slider_step">
							<xsl:variable name="slider_marks_count" select="round( ( $slider_max - $slider_min ) div $slider_step )" />
							<xsl:text>{dValue:</xsl:text>
							<xsl:value-of select="$slider_min" />
							<xsl:text>,dPercent:0</xsl:text>
							<xsl:text>,dStep:</xsl:text>
							<xsl:value-of select="$slider_step" />
							<xsl:text>,sLabel:'</xsl:text>
							<xsl:choose>
								<xsl:when test="$data[text() = 'min_label']">
									<xsl:value-of select="$data[text() = 'min_label']/following-sibling::*[1][name() = 'dd']" />
								</xsl:when>
								<xsl:otherwise>
									<xsl:value-of select="$slider_min" />
								</xsl:otherwise>
							</xsl:choose>
							<xsl:text>'}</xsl:text>
							<xsl:for-each select="/descendant::*[$slider_marks_count > position()]">
								<xsl:variable name="slider_value" select="$slider_min + position() * $slider_step" />
								<xsl:text>, {dValue:</xsl:text>
								<xsl:value-of select="$slider_value" />
								<xsl:text>,dPercent:</xsl:text>
								<xsl:value-of select="floor( position() * 100 div $slider_marks_count )" />
								<xsl:text>,dStep:</xsl:text>
								<xsl:value-of select="$slider_step" />
								<xsl:text>,sLabel:'</xsl:text>
								<xsl:value-of select="$slider_value" />
								<xsl:text>'}</xsl:text>
							</xsl:for-each>
							<xsl:text>,{dValue:</xsl:text>
							<xsl:value-of select="$slider_max" />
							<xsl:text>,dPercent:100</xsl:text>
							<xsl:text>,dStep:</xsl:text>
							<xsl:value-of select="$slider_step" />
							<xsl:text>,sLabel:'</xsl:text>
							<xsl:choose>
								<xsl:when test="$data[text() = 'max_label']">
									<xsl:value-of select="$data[text() = 'max_label']/following-sibling::*[1][name() = 'dd']" />
								</xsl:when>
								<xsl:otherwise>
									<xsl:value-of select="$slider_max" />
								</xsl:otherwise>
							</xsl:choose>
							<xsl:text>'}</xsl:text>
						</xsl:when>
					</xsl:choose>
					<!--xsl:variable name="slider" select="$data[( text() = 'value' or text() = 'percent' or text() = 'label' or text() = 'step' )]" />
					<xsl:for-each select="$data[( text() = 'min_label' or text() = 'max_label' )]">
						<xsl:apply-templates select="." mode="f_script">
							<xsl:with-param name="name" select="concat( 's', translate( text(), 'm', 'M' ) )" />
						</xsl:apply-templates>
						<xsl:if test="position() != last() or $slider">,</xsl:if>
					</xsl:for-each>
					<xsl:for-each select="$slider">
						<xsl:value-of select="concat( 'a', translate( substring( text(), 1, 1 ), 'vpls', 'VPLS' ), substring( text(), 2 ), ':[', following-sibling::*[1][name() = 'dd'], ']' )" />
						<xsl:if test="position() != last()">,</xsl:if>
					</xsl:for-each-->
					<xsl:text>]}, </xsl:text>
				</xsl:if>
				<xsl:text>sType:'</xsl:text>
					<xsl:variable name="type" select="$data[text() = 'type']/following-sibling::*[1][name() = 'dd']" />
					<xsl:choose>
						<xsl:when test="string( $type)">
							<xsl:value-of select="$type" />
						</xsl:when>
						<xsl:when test="@type = 'file' or @type = 'submit' or @type = 'button' or @type = 'image' or @type = 'checkbox' or @type = 'radio' or @type = 'button' or starts-with( @type, 'date' ) or @type = 'phone' or @type = 'email' or @type = 'hidden'">
							<xsl:value-of select="@type" />
						</xsl:when>
						<xsl:when test="name() = 'input'">text</xsl:when>
						<xsl:when test="$slider">fieldset</xsl:when>
						<xsl:when test="name() = 'select' or name() = 'fieldset' or name() = 'textarea'"><xsl:value-of select="name()" /></xsl:when>
						<xsl:otherwise>form</xsl:otherwise>
					</xsl:choose>
				<xsl:text>'</xsl:text>
			<xsl:text>};</xsl:text>
		</script>
	</xsl:template>


	<xsl:template match="dt" mode="f_script">
		<xsl:param name="name" select="text()" />
		<xsl:value-of select="translate( $name, ' ', '_' )" />
		<xsl:choose>
			<xsl:when test="contains( text(), '_from' )">
				<xsl:text>:[</xsl:text>
				<xsl:for-each select="self::dt | following-sibling::dt[text() = current()/text()]">
					<xsl:if test="position() > 1">,</xsl:if>
					<xsl:text>[</xsl:text>
						<xsl:for-each select="following-sibling::dd[generate-id( preceding-sibling::dt[1] ) = generate-id( current() )]">
							<xsl:if test="position() > 1">,</xsl:if>
							<xsl:apply-templates select="." mode="f_script" />
						</xsl:for-each>
					<xsl:text>]</xsl:text>
				</xsl:for-each>
				<xsl:text>]</xsl:text>
			</xsl:when>
			<xsl:otherwise>
				<xsl:text>:</xsl:text>
				<xsl:apply-templates select="following-sibling::dd[generate-id( preceding-sibling::dt[1] ) = generate-id( current() )][1]" mode="f_script" />
			</xsl:otherwise>
		</xsl:choose>
	</xsl:template>


	<xsl:template match="dd" mode="f_script">
		<xsl:choose>
			<xsl:when test="not( string( . ) )">null</xsl:when>
			<xsl:when test="contains( ., '(' ) and substring-after( ., ')' ) = ''">
				<xsl:value-of select="substring-before( ., '(' )" />
			</xsl:when>
			<xsl:when test="number( . ) or . = '0' or starts-with( ., '/' )">
				<xsl:value-of select="." />
			</xsl:when>
			<xsl:otherwise>'<xsl:value-of select="translate( ., $f_apostrophe_symbol, '' )" />'</xsl:otherwise>
		</xsl:choose>
	</xsl:template>


	<!-- layout -->
	<xsl:template name="f_data">
		<xsl:param name="data" />
		<xsl:param name="parent_id" />
		<xsl:choose>
			<xsl:when test="$data[name() = 'fieldset' or name() = 'input' or name() = 'textarea' or name() = 'div' or name() = 'select']">
				<xsl:apply-templates select="$data[name() != 'label' and name() != 'dl']" mode="f_field">
					<xsl:with-param name="parent_id" select="$parent_id" />
				</xsl:apply-templates>
			</xsl:when>
			<xsl:otherwise>
				<xsl:apply-templates select="$data/*[name() != 'label' and name() != 'dl']" mode="f_field">
					<xsl:with-param name="parent_id" select="$parent_id" />
				</xsl:apply-templates>
			</xsl:otherwise>
		</xsl:choose>
	</xsl:template>


	<xsl:template match="div" mode="f_field">
		<div>
			<xsl:copy-of select="@class | @id" />
			<xsl:apply-templates mode="html" />
		</div>
	</xsl:template>


	<xsl:template match="style" mode="f_field">
		<style type="text/css" media="all">
			<xsl:copy-of select="@type | @media" />
			<xsl:apply-templates mode="common_script" />
		</style>
	</xsl:template>


	<xsl:template match="fieldset | input | select | textarea" mode="f_field">
		<xsl:param name="parent_id" />
		<xsl:param name="position" select="0" />
		<xsl:param name="repeat_template" />
		<xsl:param name="reset_value" />
		<xsl:variable name="repeat" select="following-sibling::*[1][name() = 'dl']/dt[starts-with( ., 'repeat')][1]" />
		<xsl:choose>
			<xsl:when test="$repeat">
				<xsl:variable name="repeat_name" select="$repeat/following-sibling::*[1][name() = 'dd']/text()" />
				<xsl:variable name="preceding_repetition" select="preceding-sibling::dl/dt[starts-with( ., 'repeat' )]/following-sibling::*[1][name() = 'dd'][text() = $repeat_name]" />
				<xsl:apply-templates select="." mode="f_field_layout">
					<xsl:with-param name="parent_id" select="$parent_id" />
					<xsl:with-param name="repeat_template" select="generate-id( $repeat[text() = 'repeat_template']/parent::dl/preceding-sibling::*[1] )" />
					<xsl:with-param name="reset_value" select="$reset_value" />
					<xsl:with-param name="position" select="count( $preceding_repetition )" />
				</xsl:apply-templates>
				<xsl:if test="not( ../dl/dt[text() = 'repeat_template']/following-sibling::*[1][name() = 'dd'][text() = $repeat_name] )
				              and not( $repeat/../following-sibling::dl/dt[starts-with( ., 'repeat' )]/following-sibling::*[1][name() = 'dd'][text() = $repeat_name] )">
					<xsl:apply-templates select="." mode="f_field_layout">
						<xsl:with-param name="parent_id" select="$parent_id" />
						<xsl:with-param name="repeat_template" select="generate-id( . )" />
						<xsl:with-param name="reset_value" select="'true'" />
						<xsl:with-param name="position" select="count( $preceding_repetition ) + 1" />
					</xsl:apply-templates>
				</xsl:if>
			</xsl:when>
			<xsl:otherwise>
				<xsl:apply-templates select="." mode="f_field_layout">
					<xsl:with-param name="parent_id" select="$parent_id" />
					<xsl:with-param name="position" select="$position" />
					<xsl:with-param name="repeat_template" select="$repeat_template" />
					<xsl:with-param name="reset_value" select="$reset_value" />
				</xsl:apply-templates>
			</xsl:otherwise>
		</xsl:choose>
	</xsl:template>


	<xsl:template match="fieldset" mode="f_field_layout">
		<xsl:param name="parent_id" />
		<xsl:param name="repeat_template" />
		<xsl:param name="reset_value" />
		<xsl:param name="position" select="0" />
		<xsl:variable name="id">
			<xsl:apply-templates select="." mode="f_get_ID">
				<xsl:with-param name="position" select="$position" />
			</xsl:apply-templates>
		</xsl:variable>
		<xsl:variable name="params" select="following-sibling::*[1][name() = 'dl']" />
		<!--xsl:if test="$params or contains( @class, 'sheet' )"-->
			<xsl:apply-templates select="." mode="f_script">
				<xsl:with-param name="id" select="$id" />
				<xsl:with-param name="parent_id" select="$parent_id" />
				<xsl:with-param name="repeat_template" select="$repeat_template" />
				<xsl:with-param name="reset_value" select="$reset_value" />
				<xsl:with-param name="slider" select="$params/dt[text() = 'slider']" />
			</xsl:apply-templates>
		<!--/xsl:if-->
		<xsl:if test="( contains( @class, 'sheet' ) and not( contains( @class, 'tabs_sheet' ) ) )">
			<xsl:if test="not( preceding-sibling::fieldset[( contains( @class, 'sheet' ) and not( contains( @class, 'tabs_sheet' ) ) )] )">
				<xsl:text disable-output-escaping="yes"><![CDATA[<div id="]]></xsl:text>
				<xsl:value-of select="concat( $f_ghost_id_prefix, generate-id(.) )" />
				<xsl:text disable-output-escaping="yes"><![CDATA[" class="sheets">]]></xsl:text>
			</xsl:if>
			<xsl:apply-templates select="legend" mode="f_legend">
				<xsl:with-param name="id" select="$id" />
			</xsl:apply-templates>
		</xsl:if>
		<div id="{$id}">
			<xsl:attribute name="class">
				<xsl:text>fieldset</xsl:text>
				<xsl:if test="( contains( @class, 'sheet' ) and not( contains( @class, 'tabs_sheet' ) ) ) and not( preceding-sibling::fieldset[( contains( @class, 'sheet' ) and not( contains( @class, 'tabs_sheet' ) ) )] | ../fieldset[contains( @class, 'selected' )] )"> selected</xsl:if>
				<xsl:if test="string( @class )"><xsl:value-of select="concat( ' ', @class )" /></xsl:if>
				<xsl:if test="$repeat_template = generate-id( . )"> repeat_template</xsl:if>
				<xsl:if test="$params/dt[starts-with( ., 'required')][1]"> required</xsl:if>
			</xsl:attribute>
			<xsl:if test="not( ( contains( @class, 'sheet' ) and not( contains( @class, 'tabs_sheet' ) ) ) )">
				<xsl:apply-templates select="legend" mode="f_legend">
					<xsl:with-param name="id" select="$id" />
				</xsl:apply-templates>
			</xsl:if>
			<xsl:apply-templates select="." mode="f_field_layout_inner">
				<xsl:with-param name="id" select="$id" />
				<xsl:with-param name="parent_id" select="$parent_id" />
				<xsl:with-param name="repeat_template" select="$repeat_template" />
				<xsl:with-param name="reset_value" select="$reset_value" />
				<xsl:with-param name="position" select="$position" />
			</xsl:apply-templates>
			<xsl:apply-templates select="$params/dt[starts-with( ., 'repeat' )][1]" mode="f_repeat_control">
				<xsl:with-param name="id" select="$id" />
			</xsl:apply-templates>
			<xsl:apply-templates select="self::*[( contains( @class, 'sheet' ) and not( contains( @class, 'tabs_sheet' ) ) )]" mode="f_sheet_control">
				<xsl:with-param name="id" select="$id" />
			</xsl:apply-templates>
			<xsl:apply-templates select="$params/dt[text() = 'required_message'][1]/following-sibling::*[1][name() = 'dd']" mode="f_required_message" />
			<xsl:apply-templates select="$params/dt[contains( text(), 'valid_message' )][1]/following-sibling::*[1][name() = 'dd']" mode="f_valid_message" />
		</div>
		<xsl:if test="( contains( @class, 'sheet' ) and not( contains( @class, 'tabs_sheet' ) ) ) and not( following-sibling::fieldset[( contains( @class, 'sheet' ) and not( contains( @class, 'tabs_sheet' ) ) )] )">
			<xsl:text disable-output-escaping="yes"><![CDATA[</div>]]></xsl:text>
		</xsl:if>
	</xsl:template>


	<xsl:template match="fieldset" mode="f_field_layout_inner">
		<xsl:param name="id" />
		<xsl:param name="parent_id" />
		<xsl:param name="repeat_template" />
		<xsl:param name="reset_value" />
		<xsl:param name="position" select="0" />
		<div class="reducer">
			<xsl:apply-templates select="fieldset | input | textarea | select | div | style" mode="f_field">
				<xsl:with-param name="parent_id" select="$id" />
				<xsl:with-param name="position" select="$position" />
				<xsl:with-param name="repeat_template" select="$repeat_template" />
				<xsl:with-param name="reset_value" select="$reset_value" />
			</xsl:apply-templates>
		</div>
	</xsl:template>


	<xsl:template match="*" mode="f_field_layout" />


	<xsl:template match="input | select | textarea" mode="f_field_layout">
		<xsl:param name="parent_id" />
		<xsl:param name="repeat_template" />
		<xsl:param name="reset_value" />
		<xsl:param name="position" select="0" />
		<xsl:if test="not( preceding-sibling::*[2 > position()][name() = 'dl'][dd = 'combobox'] )">
			<xsl:variable name="name">
				<xsl:apply-templates select="." mode="f_get_name">
					<xsl:with-param name="position" select="$position" />
				</xsl:apply-templates>
			</xsl:variable>
			<xsl:variable name="id">
				<xsl:apply-templates select="." mode="f_get_ID">
					<xsl:with-param name="position" select="$position" />
				</xsl:apply-templates>
			</xsl:variable>
			<xsl:variable name="params" select="following-sibling::*[1][name() = 'dl']" />
			<xsl:variable name="combobox" select="$params[dd = 'combobox']/following-sibling::*[2 > position()][name() = 'select']" />
			<xsl:variable name="parent_id1">
				<xsl:choose>
					<xsl:when test="$params/dt[text() = 'slider']">
						<xsl:value-of select="concat( $f_ghost_id_prefix, generate-id(.) )" />
					</xsl:when>
					<xsl:otherwise>
						<xsl:value-of select="$parent_id" />
					</xsl:otherwise>
				</xsl:choose>
			</xsl:variable>
			<xsl:if test="$params/dt[text() = 'slider']">
				<xsl:apply-templates select="." mode="f_script">
					<xsl:with-param name="id" select="$parent_id1" />
					<xsl:with-param name="parent_id" select="$parent_id" />
					<xsl:with-param name="repeat_template" select="$repeat_template" />
					<xsl:with-param name="reset_value" select="$reset_value" />
					<xsl:with-param name="slider" select="1" />
				</xsl:apply-templates>
				<xsl:text disable-output-escaping="yes"><![CDATA[<div id="]]></xsl:text>
				<xsl:value-of select="$parent_id1" />
				<xsl:text disable-output-escaping="yes"><![CDATA[">]]></xsl:text>
			</xsl:if>
			<!--xsl:if test="$params or @type = 'submit' or ancestor::form[1]/descendant::dd[text() = current()/@name]"-->
				<xsl:apply-templates select="." mode="f_script">
					<xsl:with-param name="id" select="$id" />
					<xsl:with-param name="row_id" select="concat( $f_row_id_prefix, $id )" />
					<xsl:with-param name="parent_id" select="$parent_id1" />
					<xsl:with-param name="repeat_template" select="$repeat_template" />
					<xsl:with-param name="reset_value" select="$reset_value" />
					<xsl:with-param name="position" select="$position" />
					<xsl:with-param name="combobox" select="$combobox" />
				</xsl:apply-templates>
			<!--/xsl:if-->
			<dl id="{concat( $f_row_id_prefix, $id )}">
				<xsl:attribute name="class">
					<xsl:text>row for_</xsl:text>
					<xsl:choose>
						<xsl:when test="@type = 'checkbox' or @type = 'radio'">selector</xsl:when>
						<xsl:when test="$combobox">combobox</xsl:when>
						<xsl:otherwise>
							<xsl:value-of select="name()" />
						</xsl:otherwise>
					</xsl:choose>
					<xsl:if test="string( @type )"> type_<xsl:value-of select="@type" /></xsl:if>
					<xsl:if test="string( @class )"><xsl:value-of select="concat( ' ', @class )" /></xsl:if>
					<!--xsl:if test="string( $repeat_template )"> repeat_template</xsl:if-->
					<xsl:if test="$params/dt[starts-with( ., 'required')][1]"> required</xsl:if>
					<xsl:if test="$repeat_template = generate-id( . )"> repeat_template</xsl:if>
				</xsl:attribute>
				<dt class="label">
					<xsl:apply-templates select="preceding-sibling::*[1][name() = 'label']" mode="f_label">
						<xsl:with-param name="for" select="$id" />
					</xsl:apply-templates>
					<xsl:apply-templates select="$params/dt[text() = 'comment']/following-sibling::*[1][name() = 'dd']" mode="f_comment" />
				</dt>
				<dd class="field">
					<xsl:apply-templates select="." mode="f_control">
						<xsl:with-param name="id" select="$id" />
						<xsl:with-param name="name" select="$name" />
						<xsl:with-param name="position" select="$position" />
						<xsl:with-param name="repeat_template" select="$repeat_template" />
						<xsl:with-param name="reset_value" select="$reset_value" />
					</xsl:apply-templates>
					<xsl:apply-templates select="$combobox" mode="f_control">
						<xsl:with-param name="id">
							<xsl:apply-templates select="$combobox" mode="f_get_ID">
								<xsl:with-param name="position" select="$position" />
							</xsl:apply-templates>
						</xsl:with-param>
						<xsl:with-param name="name">
							<xsl:apply-templates select="$combobox" mode="f_get_name">
								<xsl:with-param name="position" select="$position" />
							</xsl:apply-templates>
						</xsl:with-param>
						<xsl:with-param name="position" select="$position" />
					</xsl:apply-templates>
					<xsl:apply-templates select="$params/dt[text() = 'example']/following-sibling::*[1][name() = 'dd']" mode="f_example" />
					<xsl:apply-templates select="$params/dt[starts-with( ., 'repeat' )][1]" mode="f_repeat_control">
						<xsl:with-param name="id" select="$id" />
					</xsl:apply-templates>
					<xsl:apply-templates select="$params/dt[text() = 'required_message'][1]/following-sibling::*[1][name() = 'dd']" mode="f_required_message" />
					<xsl:apply-templates select="$params/dt[contains( text(), 'valid_message' )][1]/following-sibling::*[1][name() = 'dd']" mode="f_valid_message" />
				</dd>
			</dl>
			<xsl:if test="$params/dt[text() = 'slider']">
				<xsl:text disable-output-escaping="yes"><![CDATA[</div>]]></xsl:text>
			</xsl:if>
		</xsl:if>
	</xsl:template>


	<!-- hidden -->
	<xsl:template match="input[@type = 'hidden']" mode="f_field_layout">
		<xsl:param name="parent_id" />
		<xsl:param name="position" select="0" />
		<xsl:param name="repeat_template" />
		<xsl:variable name="id">
			<xsl:apply-templates select="." mode="f_get_ID">
				<xsl:with-param name="position" select="$position" />
			</xsl:apply-templates>
		</xsl:variable>
		<xsl:variable name="name">
			<xsl:apply-templates select="." mode="f_get_name">
				<xsl:with-param name="position" select="$position" />
			</xsl:apply-templates>
		</xsl:variable>
		<xsl:apply-templates select="." mode="f_script">
			<xsl:with-param name="id" select="$id" />
			<xsl:with-param name="parent_id" select="$parent_id" />
			<xsl:with-param name="repeat_template" select="$repeat_template" />
			<xsl:with-param name="position" select="$position" />
		</xsl:apply-templates>
		<input type="hidden" name="{$name}" id="{$id}">
			<xsl:apply-templates select="." mode="f_control_value" />
		</input>
	</xsl:template>


	<!-- repeat & sheet control -->
	<xsl:template match="*" mode="f_repeat_control">
		<xsl:param name="id" />
		<div class="repeat_control">
			<input type="button" class="button append" id="{concat( $f_repeat_append_prefix, $id )}">
				<xsl:attribute name="value">
					<xsl:choose>
						<xsl:when test="../dt[text() = 'append']"><xsl:apply-templates select="../dt[text() = 'append']/following-sibling::*[1][name() = 'dd']/node()" mode="html" /></xsl:when>
						<xsl:otherwise>+</xsl:otherwise>
					</xsl:choose>
				</xsl:attribute>
			</input>
			<input type="button" class="button remove" id="{concat( $f_repeat_remove_prefix, $id )}">
				<xsl:attribute name="value">
					<xsl:choose>
						<xsl:when test="../dt[text() = 'remove']"><xsl:apply-templates select="../dt[text() = 'remove']/following-sibling::*[1][name() = 'dd']/node()" mode="html" /></xsl:when>
						<xsl:otherwise>&#8722;</xsl:otherwise>
					</xsl:choose>
				</xsl:attribute>
			</input>
		</div>
	</xsl:template>


	<xsl:template match="*" mode="f_sheet_control">
		<xsl:param name="id" />
		<xsl:variable name="params" select="following-sibling::*[1][name() = 'dl']" />
		<div class="sheet_control">
			<xsl:if test="preceding-sibling::*[name() = name( current() ) and ( contains( @class, 'sheet' ) and not( contains( @class, 'tabs_sheet' ) ) )]">
				<input type="button" class="button prev" id="{concat( $f_sheet_previous_prefix, $id )}">
					<xsl:attribute name="value">
						<xsl:choose>
							<xsl:when test="$params/dt[text() = 'prev']"><xsl:apply-templates select="$params/dt[text() = 'prev']/following-sibling::*[1][name() = 'dd']/node()" mode="html" /></xsl:when>
							<xsl:otherwise>&#8592;</xsl:otherwise>
						</xsl:choose>
					</xsl:attribute>
				</input>
			</xsl:if>
			<xsl:if test="following-sibling::*[name() = name( current() ) and ( contains( @class, 'sheet' ) and not( contains( @class, 'tabs_sheet' ) ) )]">
				<input type="button" class="button next" id="{concat( $f_sheet_next_prefix, $id )}">
					<xsl:attribute name="value">
						<xsl:choose>
							<xsl:when test="$params/dt[text() = 'next']"><xsl:apply-templates select="$params/dt[text() = 'next']/following-sibling::*[1][name() = 'dd']/node()" mode="html" /></xsl:when>
							<xsl:otherwise>&#8594;</xsl:otherwise>
						</xsl:choose>
					</xsl:attribute>
				</input>
			</xsl:if>
		</div>
	</xsl:template>


	<!-- buttons -->
	<xsl:template match="input[@type = 'submit' or @type = 'SUBMIT' or @type = 'Submit' or @type = 'button'] | button" mode="f_control">
		<input type="button" class="button" value="{label/text() | @value | value/text()}">
			<xsl:attribute name="id"><xsl:apply-templates select="." mode="f_get_ID" /></xsl:attribute>
			<xsl:copy-of select="@type | @name | @disabled | ancestor::*/@disabled | @onclick" />
		</input>
	</xsl:template>


	<!-- file -->
	<xsl:template match="input[@type = 'file']" mode="f_control">
		<xsl:param name="id" />
		<xsl:param name="name" />
		<input type="file" name="{$name}" id="{$id}">
			<xsl:attribute name="class">
				<xsl:text>file</xsl:text>
				<xsl:if test="string( @class )">
					<xsl:value-of select="concat( ' ', @class )" />
				</xsl:if>
			</xsl:attribute>
		</input>
	</xsl:template>


	<!-- input & textarea -->
	<xsl:template match="input | textarea" mode="f_control">
		<xsl:param name="id" />
		<xsl:param name="name" />
		<xsl:param name="repeat_template" />
		<xsl:param name="reset_value" />
		<xsl:variable name="size">
			<xsl:choose>
				<xsl:when test="@size"><xsl:value-of select="@size" /></xsl:when>
				<xsl:when test="@type = 'number'">10</xsl:when>
				<xsl:when test="@type = 'phone'">18</xsl:when>
				<xsl:when test="@type = 'date'">10</xsl:when>
			</xsl:choose>
		</xsl:variable>
		<xsl:element name="{name()}">
			<xsl:attribute name="name"><xsl:value-of select="$name" /></xsl:attribute>
			<xsl:attribute name="id"><xsl:value-of select="$id" /></xsl:attribute>
			<xsl:copy-of select="@maxlength | @readonly | @disabled | @placeholder | @type[ . = 'password' ] | @autocomplete" />
			<xsl:if test="@type = 'password'">
				<xsl:attribute name="autocomplete">off</xsl:attribute>
			</xsl:if>
			<xsl:attribute name="class">
				<xsl:text>text</xsl:text>
				<xsl:if test="number( $size ) or @cols or @rows"> presized</xsl:if>
				<xsl:if test="string( @class )">
					<xsl:value-of select="concat( ' ', @class )" />
				</xsl:if>
			</xsl:attribute>
			<xsl:choose>
				<xsl:when test="self::input and number( $size )">
					<xsl:attribute name="size">
						<xsl:value-of select="$size" />
					</xsl:attribute>
				</xsl:when>
				<xsl:when test="self::textarea">
					<xsl:attribute name="cols">30</xsl:attribute>
					<xsl:copy-of select="@cols" />
					<xsl:attribute name="rows">5</xsl:attribute>
					<xsl:copy-of select="@rows" />
				</xsl:when>
			</xsl:choose>
			<xsl:if test="number( $size ) or number( @cols ) or number( @rows )">
				<xsl:attribute name="style">
					<xsl:if test="number( $size )">width:<xsl:value-of select="$size div 1.5" />em;</xsl:if>
					<xsl:if test="number( @cols )">width:<xsl:value-of select="@cols div 1.5" />em;</xsl:if>
					<xsl:if test="number( @rows )">height:<xsl:value-of select="@rows * 1.5" />em;</xsl:if>
				</xsl:attribute>
			</xsl:if>
			<xsl:if test="not( string( $reset_value ) )">
				<xsl:apply-templates select="." mode="f_control_value" />
			</xsl:if>
		</xsl:element>
	</xsl:template>


	<xsl:template match="input" mode="f_control_value">
		<xsl:attribute name="value"><xsl:value-of select="@value" /></xsl:attribute>
	</xsl:template>


	<xsl:template match="textarea" mode="f_control_value">
		<xsl:apply-templates select="text() | *" mode="f_html_to_text" />
		<xsl:text><![CDATA[]]></xsl:text>
	</xsl:template>


	<!-- html to text -->
	<xsl:template match="*" mode="f_html_to_text">
		<xsl:value-of select="concat( '&lt;', name() )" />
		<xsl:for-each select="@*">
			<xsl:value-of select="concat( ' ', name(), '=&quot;' )" />
			<xsl:value-of select="concat( ., '&quot;' )" />
		</xsl:for-each>
		<xsl:choose>
			<xsl:when test="* or string( text() )">
				<xsl:text>&gt;</xsl:text>
				<xsl:apply-templates mode="f_html_to_text" />
				<xsl:value-of select="concat( '&lt;/', name() )" />
			</xsl:when>
			<xsl:otherwise> /</xsl:otherwise>
		</xsl:choose>
		<xsl:text>&gt;</xsl:text>
	</xsl:template>


	<!-- select -->
	<xsl:template match="select" mode="f_control">
		<xsl:param name="id" />
		<xsl:param name="name" />
		<xsl:param name="position" />
		<xsl:choose>
			<xsl:when test="not( @type = 'checkbox' or @type = 'radio' ) or preceding-sibling::*[2 > position()][name() = 'dl'][dd = 'combobox']">
				<xsl:apply-templates select="." mode="f_control_select">
					<xsl:with-param name="id" select="$id" />
					<xsl:with-param name="name" select="$name" />
					<xsl:with-param name="position" select="$position" />
				</xsl:apply-templates>
			</xsl:when>
			<xsl:otherwise>
				<xsl:apply-templates select="." mode="f_control_radiobox">
					<xsl:with-param name="id" select="$id" />
					<xsl:with-param name="name" select="$name" />
					<xsl:with-param name="position" select="$position" />
				</xsl:apply-templates>
			</xsl:otherwise>
		</xsl:choose>
	</xsl:template>


	<xsl:template match="select" mode="f_control_select">
		<xsl:param name="id" />
		<xsl:param name="name" />
		<select name="{$name}" id="{$id}">
			<xsl:copy-of select="@multiple | @size" />
			<xsl:choose>
				<xsl:when test="not( descendant::option[@selected] )">
					<xsl:apply-templates select="option | optgroup" mode="f_select_option">
						<xsl:with-param name="selected" select="generate-id( descendant::option[1] )" />
					</xsl:apply-templates>
				</xsl:when>
				<xsl:otherwise>
					<xsl:apply-templates select="option | optgroup" mode="f_select_option" />
				</xsl:otherwise>
			</xsl:choose>
		</select>
	</xsl:template>


	<xsl:template match="optgroup" mode="f_select_option">
		<xsl:param name="selected" />
		<optgroup>
			<xsl:copy-of select="@label | @class" />
			<xsl:apply-templates select="option | optgroup" mode="f_select_option">
				<xsl:with-param name="selected" select="$selected" />
			</xsl:apply-templates>
		</optgroup>
	</xsl:template>


	<xsl:template match="option" mode="f_select_option">
		<xsl:param name="selected" />
		<xsl:variable name="option_is_selected">
			<xsl:apply-templates select="." mode="f_select_option_is_selected">
				<xsl:with-param name="selected" select="$selected" />
			</xsl:apply-templates>
		</xsl:variable>
		<option>
			<xsl:copy-of select="@value | @class" />
			<xsl:if test="not( @value )">
				<xsl:attribute name="value">
					<xsl:value-of select="." />
				</xsl:attribute>
			</xsl:if>
			<xsl:if test="$option_is_selected = 1">
				<xsl:attribute name="class">initial_selected<xsl:if test="string( normalize-space( @class ) )"><xsl:value-of select="concat( ' ', normalize-space( @class ) )" /></xsl:if></xsl:attribute>
				<xsl:attribute name="selected">true</xsl:attribute>
			</xsl:if>
			<xsl:value-of select="." />
		</option>
	</xsl:template>


	<!-- radio & checkbox -->
	<xsl:template match="select[@type = 'checkbox' or @type = 'radio']" mode="f_control_radiobox">
		<xsl:param name="name" />
		<xsl:param name="position" select="0" />
		<xsl:choose>
			<xsl:when test="@type = 'radio' and not( descendant::option[@selected] )">
				<xsl:apply-templates select="option | optgroup" mode="f_selector">
					<xsl:with-param name="name" select="$name" />
					<xsl:with-param name="position" select="$position" />
					<xsl:with-param name="selected" select="generate-id( descendant::option[1] )" />
				</xsl:apply-templates>
			</xsl:when>
			<xsl:otherwise>
				<xsl:apply-templates select="option | optgroup" mode="f_selector">
					<xsl:with-param name="name" select="$name" />
					<xsl:with-param name="position" select="$position" />
				</xsl:apply-templates>
			</xsl:otherwise>
		</xsl:choose>
	</xsl:template>


	<xsl:template match="optgroup" mode="f_selector">
		<xsl:param name="name" />
		<xsl:param name="position" select="0" />
		<xsl:param name="selected" />
		<div>
			<xsl:attribute name="class">optgroup<xsl:if test="string( normalize-space( @class ) )"><xsl:value-of select="concat( ' ', normalize-space( @class ) )" /></xsl:if></xsl:attribute>
			<xsl:if test="string( @label )">
				<strong class="label"><xsl:value-of select="@label" /></strong>
			</xsl:if>
			<xsl:apply-templates select="option | optgroup" mode="f_selector">
				<xsl:with-param name="name" select="$name" />
				<xsl:with-param name="position" select="$position" />
				<xsl:with-param name="selected" select="$selected" />
			</xsl:apply-templates>
		</div>
	</xsl:template>


	<xsl:template match="option" mode="f_selector">
		<xsl:param name="name" />
		<xsl:param name="position" select="0" />
		<xsl:param name="selected" />
		<xsl:variable name="id">
			<xsl:apply-templates select="." mode="f_get_ID">
				<xsl:with-param name="position" select="$position" />
			</xsl:apply-templates>
		</xsl:variable>
		<xsl:variable name="option_is_selected">
			<xsl:apply-templates select="." mode="f_select_option_is_selected">
				<xsl:with-param name="selected" select="$selected" />
			</xsl:apply-templates>
		</xsl:variable>
		<div id="{concat( $f_row_id_prefix, $id )}">
			<xsl:attribute name="class">
				<xsl:text>selector</xsl:text>
				<xsl:if test="string( normalize-space( @class ) )"><xsl:value-of select="concat( ' ', normalize-space( @class ) )" /></xsl:if>
				<xsl:if test="$option_is_selected = 1"> initial_selected</xsl:if>
			</xsl:attribute>
			<input name="{$name}" type="{ancestor::select[1]/@type}" id="{$id}" value="{@value}">
				<xsl:if test="not( string( @value ) )">
					<xsl:attribute name="value"><xsl:value-of select="." /></xsl:attribute>
				</xsl:if>
				<xsl:if test="$option_is_selected = 1">
					<xsl:attribute name="checked">true</xsl:attribute>
				</xsl:if>
				<xsl:copy-of select="ancestor::select[1]/@disabled | @disabled" />
			</input>
			<!--span class="label"-->
				<xsl:apply-templates select="." mode="f_label">
					<xsl:with-param name="for" select="$id" />
					<xsl:with-param name="field" select="ancestor::select[1] | ." />
				</xsl:apply-templates>
			<!--/span-->
		</div>
		<xsl:value-of select="' '" />
	</xsl:template>


	<xsl:template match="option" mode="f_select_option_is_selected">
		<xsl:param name="selected" />
		<xsl:if test="@selected or $selected = generate-id()">1</xsl:if>
	</xsl:template>


	<xsl:template match="input[@type = 'checkbox']" mode="f_control">
		<div>Transform to &lt;select type="checkbox" multiple="true"&gt;</div>
	</xsl:template>


	<xsl:template match="input[@type = 'radio']" mode="f_control">
		<div>Transform to &lt;select type="radio"&gt;</div>
	</xsl:template>


	<!-- required & valid message, legend, label, comment & example -->
	<xsl:template match="*" mode="f_required_message">
		<div class="required_message">
			<xsl:apply-templates mode="html" />
		</div>
	</xsl:template>


	<xsl:template match="*" mode="f_valid_message">
		<div class="valid_message">
			<xsl:apply-templates mode="html" />
		</div>
	</xsl:template>


	<xsl:template match="*" mode="f_label">
		<xsl:param name="for" />
		<xsl:param name="field" />
		<label for="{$for}">
			<xsl:copy-of select="@id" />
			<xsl:if test="$field">
				<xsl:for-each select="$field">
					<xsl:copy-of select="@disabled" />
				</xsl:for-each>
			</xsl:if>
			<xsl:apply-templates mode="f_labels" />
		</label>
	</xsl:template>


	<xsl:template match="*" mode="f_comment">
		<xsl:value-of select="' '" />
		<ins class="comment {@class}">
			<xsl:copy-of select="@id" />
			<xsl:apply-templates mode="f_labels" />
		</ins>
	</xsl:template>


	<xsl:template match="*" mode="f_example">
		<xsl:value-of select="' '" />
		<ins class="example {@class}">
			<xsl:copy-of select="@id" />
			<xsl:apply-templates mode="f_labels" />
		</ins>
	</xsl:template>


	<xsl:template match="*" mode="f_legend">
		<xsl:param name="id" />
		<xsl:value-of select="' '" />
		<strong>
			<xsl:attribute name="class">
				<xsl:text>legend</xsl:text>
				<xsl:if test="string( @class )"><xsl:value-of select="concat( ' ', @class)" /></xsl:if>
			</xsl:attribute>
			<xsl:if test="contains( parent::fieldset/@class, 'sheet' )">
				<xsl:attribute name="id">
					<xsl:value-of select="concat( $f_legend_prefix, $id )" />
				</xsl:attribute>
			</xsl:if>
			<xsl:apply-templates mode="f_labels" />
		</strong>
	</xsl:template>


	<xsl:template match="*" mode="f_labels">
		<xsl:apply-templates select="." mode="html_inline" />
	</xsl:template>


	<!-- id & name -->
	<xsl:template match="*" mode="f_get_name">
		<xsl:param name="position" select="0" />
		<xsl:apply-templates select="." mode="f_get_name_inner">
			<xsl:with-param name="position" select="$position" />
		</xsl:apply-templates>
	</xsl:template>


	<xsl:template match="option" mode="f_get_name">
		<xsl:param name="position" select="0" />
		<xsl:apply-templates select="ancestor::select[1]" mode="f_get_name_inner">
			<xsl:with-param name="position" select="$position" />
		</xsl:apply-templates>
	</xsl:template>


	<xsl:template match="*" mode="f_get_name_inner">
		<xsl:param name="position" select="0" />
		<xsl:choose>
			<xsl:when test="string( @name )"><xsl:value-of select="@name" /></xsl:when>
			<xsl:when test="string( @id )"><xsl:value-of select="@id" /></xsl:when>
			<xsl:otherwise><xsl:value-of select="generate-id( . )" /></xsl:otherwise>
		</xsl:choose>
		<xsl:if test="number( $position )">
			<xsl:value-of select="concat( '_', $position )" />
		</xsl:if>
	</xsl:template>


	<xsl:template match="*" mode="f_get_ID">
		<xsl:param name="position" select="0" />
		<xsl:apply-templates select="." mode="f_get_ID_inner">
			<xsl:with-param name="position" select="$position" />
		</xsl:apply-templates>
		<xsl:if test="number( $position )">
			<xsl:value-of select="concat( $f_repeat_suffix, $position )" />
		</xsl:if>
	</xsl:template>


	<xsl:template match="option" mode="f_get_ID">
		<xsl:param name="position" select="0" />
		<xsl:choose>
			<xsl:when test="string( @id )">
				<xsl:value-of select="@id" />
			</xsl:when>
			<xsl:otherwise>
				<xsl:apply-templates select="ancestor::select[1]" mode="f_get_ID_inner">
					<xsl:with-param name="position" select="$position" />
				</xsl:apply-templates>
				<xsl:value-of select="concat( $f_option_suffix, count( preceding::option[ancestor::select[1] = current()/ancestor::select[1]] ) )" />
			</xsl:otherwise>
		</xsl:choose>
		<xsl:if test="number( $position )">
			<xsl:value-of select="concat( $f_repeat_suffix, $position )" />
		</xsl:if>
	</xsl:template>


	<xsl:template match="*" mode="f_get_ID_inner">
		<xsl:param name="position" select="0" />
		<xsl:variable name="repeat" select="following-sibling::*[1][name() = 'dl']/dt[text() = 'repeat' or text() = 'repeat_template']/following-sibling::*[1][name() = 'dd']" />
		<xsl:choose>
			<xsl:when test="string( @id )"><xsl:value-of select="@id" /></xsl:when>
			<xsl:when test="string( $repeat )"><xsl:value-of select="$repeat" /></xsl:when>
			<xsl:otherwise>
				<xsl:choose>
					<xsl:when test="name() = 'fieldset'">
						<xsl:value-of select="$f_fieldset_id_prefix" />
					</xsl:when>
					<xsl:when test="name() = 'form'">form_</xsl:when>
					<xsl:otherwise><xsl:value-of select="$f_field_id_prefix" /></xsl:otherwise>
				</xsl:choose>
				<!--xsl:value-of select="generate-id( . )" /-->
				<xsl:choose>
					<xsl:when test="string( @name )"><xsl:value-of select="@name" /></xsl:when>
					<xsl:otherwise><xsl:value-of select="generate-id( . )" /></xsl:otherwise>
				</xsl:choose>
			</xsl:otherwise>
		</xsl:choose>
	</xsl:template>


</xsl:stylesheet>
